package com.sailfish.springbootdemo.utils.redis;

import com.sailfish.springbootdemo.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCRUD {

    @Autowired
    private RedisTemplate redisTemplate;

    private Integer hours = 3; //设置有效时间3小时

    /**
     * 存储信息
     *
     * @param pcbaCompanyId
     * @param data
     */
    public void saveCmAndLineCapacityAndDeviceCapacityInfo(String pcbaCompanyId, String data) {
        System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
        System.out.println("存储工厂线体信息到redis缓存,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId);
        try {
            redisTemplate.opsForValue().set("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId, data);
            // 设置缓存时间
            redisTemplate.expire("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId, hours, TimeUnit.HOURS);
            System.out.println("存储工厂线体信息到redis缓存,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId + "->成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("存储工厂线体信息到redis缓存,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId + "->失败：" + e.getMessage());
        }
    }

    /**
     * 获取信息
     *
     * @param pcbaCompanyId
     */
    public String getCmAndLineCapacityAndDeviceCapacityInfo(String pcbaCompanyId) {
        try {
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("从redis缓存查询单板信息,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId);
            if (redisTemplate.hasKey("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId)) {
                System.out.println("从redis缓存查询单板信息,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId + "->成功");
                return (String) redisTemplate.opsForValue().get("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId);
            }
            System.out.println("从redis缓存查询单板信息,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId + "->失败：找不到对应key");
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("从redis缓存查询单板信息,key=" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId + "->失败：" + e.getMessage());
            return null;
        }
    }

    /**
     * 删除信息
     */
    public void deleteCmAndLineCapacityAndDeviceCapacityInfo(String pcbaCompanyId) {
        try {
            if (redisTemplate.hasKey("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId)) {
                redisTemplate.delete("getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId);
                System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
                System.out.println("删除redis缓存成功：" + "getAllCmAndLineCapacityAndDeviceCapacity_" + pcbaCompanyId);
            }
        } catch (Exception e) {
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("删除redis缓存失败：" +e.getMessage());
            e.printStackTrace();
        }
    }
}
